from mitmproxy.http import HTTPFlow

from zz import zz_mitmproxy, zz_log, zz_return, zz_str

log = zz_log.of()


class Counter:
    def __init__(self, callback=None):
        self.callback = callback

    def request(self, flow: HTTPFlow):
        request = flow.request  # 获取请求对象
        url = request.url
        if url.startswith('https://gzct.gzccic.com/api/member/v1/member-info/applet/member-info'):
            url_arr = url.split("/")
            member_id = url_arr[len(url_arr) - 1]
            token = request.headers.get('Access-Token')
            if zz_str.is_null_or_empty(token):
                return
            app_key = request.headers.get('Application-Key')
            r = zz_return.of(0)
            r.set_data("token", token)
            r.set_data("app_key", app_key)
            r.set_data("member_id", member_id)
            log.info(r.to_json())
            zz_mitmproxy.save_msg(r)

        print(f"response url = {request.url}")
        print(f"response headers = {request.headers}")
        print(f"request data = {request.data}")

    def response(self, flow: HTTPFlow):
        # if flow.response.status_code != 200:
        #     return
        response = flow.response  # 获取响应对象
        print(f"response content = {response.content}")


addons = [
    Counter()
]
